﻿<?php
	include '../inc/conn.php';
	$pageSize = 20;
	session_start();
	$view=0;  /*default is list*/
	extract($_POST);
	extract($_GET);
	unset($_POST,$_GET);
	if (isset($view))
	{
		if ($view == "1"){
			$view=1;
		}else{
			$view=0;
		}
	}

	if(!isset($_SESSION['logname'])){
		die("<div>User Session is missing</div>");
	}

	if (isset($act) && isset($id))
	{
		if ($act == "del"){
			$db = new mysql();
			$db->delete('daily_table', "id=".$id);
			$db->close();
			echo "<script>alert('Data has deleted!!!');window.location='admin_index.php?name=daily';</script>";
		}
	}
	
	if (!isset($page))
		$page = 1;
	
	$pageCount = 0;
	$db = new mysql();
	if (isset($_SESSION['admin']))
		$db->query("select count(id) as CNT from daily_table");
	else
		$db->query("select count(id) as CNT from daily_table where UID=".$_SESSION['ID']);
	$row = $db->fetch_assoc();
	$rowCount = $row['CNT'];
	$db->close();
	$pageCount = ceil(($rowCount/$pageSize));
	$pre = ($page-1)*$pageSize;
	
	echo '<script language="javascript">function   Go(it)   {';
	if (!isset($sort))
		echo  'window.location="admin_index.php?name=daily&page="+it.value;';
	else
		echo  "window.location='admin_index.php?name=daily&sort=$sort&page='+it.value;";
	echo "}</script>";

	echo "<div class='container-fluid'>";	
	echo "<div class='panel panel-primary'>";	
	echo "    <div class='panel-heading'>";	
	echo "        <h3 class='panel-title'>工程日报库(Engineering Daily Report System)</h3>";	
	echo "    </div>";	
	echo "    <div class='panel-body'>";	
	echo "	<div class='well'>";	
	echo "	<form class='form-inline' action='admin_index.php?name=daily' method='post'>";	
	echo "		<div style='form-group'>";	
	echo "				日报搜索：<input type='text' class='form-control' name='search'><button type='submit' class='btn btn-info'>Search</button>";	
	echo "				<div style='float:right'>操作：<a href='#' rel='tooltip' title='添加今天的日报' onclick=\"javascript:window.open('add_daily.php','日报', 'height=450, width=760, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');\"><span class=\"glyphicon glyphicon-plus\"></span> 添加</a> | 
			<a href='../export-daily.php' rel='tooltip' title='导出今天所有人的日报'><span class=\"glyphicon glyphicon-save\"></span> 导出</a></div>";	
	echo "		</div>";
	echo "	</form>";
	echo "	</div>";
	echo "	<div>";
	if ($view == 0){
		echo "<table border=0 cellpadding=0 class='table table-striped'>";
	} 
 	if (isset($_SESSION['admin'])){
		echo '<tr class="success"><th width="75%">日报内容</th>
				<th width="5%"><a href="admin_index.php?name=daily&sort=ow">负责人</a></th>
				<th width="5%"><a href="admin_index.php?name=daily&sort=st">状态</a></th>
				<th width="5%"><a href="admin_index.php?name=daily&sort=md">建立时间</a></th>
				<th width="5%"><a href="admin_index.php?name=daily&sort=ud">更新时间</a></th>
				<th width="5%">操作</th></tr>';
	}else{
		echo '<tr class="success"><th width="72%">日报内容</th><th>状态</th><th>建立时间</th><th>更新时间</th><th>操作</th></tr>';
	}
	$db = new mysql();
	
	if (isset($sort)){
		switch($sort){
			case "ud":
				$db->query("select * from daily_table ORDER BY update_date desc limit $pre,$pageSize");
				break;
			case "md":
				$db->query("select * from daily_table ORDER BY add_date desc limit $pre,$pageSize");
				break;
			case "st":
				$db->query("select * from daily_table ORDER BY status desc limit $pre,$pageSize");
				break;
			case "ow":
				$db->query("select * from daily_table ORDER BY UNAME desc limit $pre,$pageSize");
				break;
			}
	}
	else
	{
		if (isset($search))
		{
			$db->query("select * from daily_table where daily_content like '%$search%' ORDER BY add_date DESC");		
		}else if (isset($_SESSION['admin'])){
			$db->query("select * from daily_table ORDER BY add_date DESC, update_date desc limit $pre,$pageSize");
		}else{
			$db->query("select * from daily_table where UID=".$_SESSION['ID']." ORDER BY add_date desc, update_date DESC limit $pre,$pageSize");
		}
	}
	
	$num = $db->db_num_rows();
	for($i=0; $i<$num; $i++){
		$row = $db->fetch_assoc();
		if ($view == 0){
			$item = $i + 1;
			echo "<tr>";
			echo "<td>".htmlspecialchars_decode($row['daily_content'])."</td>";
			if (isset($_SESSION['admin'])) echo "<td>".$row['UNAME']."</td>";
			if ($row['status'] == "Open") 
				echo "<td><font color='RED'>".$row['status']."</font></td>";
			if ($row['status'] == "Tracking") 
				echo "<td><font color='BLUE'>".$row['status']."</font></td>";
			if ($row['status'] == "Close") 
				echo "<td>".$row['status']."</td>";
			echo "<td>".substr($row['add_date'],0,10)."</td>";
			echo "<td>".substr($row['update_date'],0,10)."</td>";
			echo "<td><a href=\"#\" onclick=\"javascript:window.open('mod_daily.php?id=".$row['ID']."','更新日报', 'height=450, width=1007, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');\"><span class=\"glyphicon glyphicon-edit\"></span></a>";
			if (isset($_SESSION['admin']))
				echo '&nbsp;|&nbsp;<a href="daily_report.php?id='.$row['ID'].'&act=del" rel="tooltip" title="删除表内容，当心哦~~~"><font color="red"><span class="glyphicon glyphicon-remove"></span></font></a></td>';
			echo "</td></tr>";			
		}else{
			echo "<div class='alert alert-info'><h3>".htmlspecialchars_decode($row['daily_content'])."</h3><strong>编写：</strong>".htmlspecialchars_decode($row['status'])."&nbsp;&nbsp;&nbsp;&nbsp;<strong>编写日期：</strong>".htmlspecialchars_decode($row['add_date'])."</div>";
			echo "<div class='well'>".htmlspecialchars_decode($row['update_date'])."</div>";
		}
	}	
	if ($view == 0)
		echo "</table>";
		echo "</div>";
		if($pageCount>1) { 
			echo "<div align='center'><ul class='pager'>";
	?>
	<li class='previous'><a href="admin_index.php?name=daily&page=<?if($page>=2) echo $page-1; else echo $page; if(isset($sort)) echo "&sort=$sort"; ?>">&larr;上一页</a></li>
	<? 	
			echo "第<select name='page' id='page' onchange='javascript:Go(this);'>";
			for($j=1;$j<=$pageCount;$j++){
				if($j==$page)
					echo "<option value='$j' selected='$j'>$j</option>";
				else
					echo "<option value='$j'>$j</option>";
			}
			echo "</select>页";
	?>
	<li class='next'><a href="admin_index.php?name=daily&page=<?if($page<$pageCount) echo $page+1; else echo 1; if(isset($sort)) echo "&sort=$sort"; ?>">下一页&rarr;</a></li>
	<? 
			echo "</ul></div>";
		} 
		echo "</div>";
		echo "</div>";
		echo "</div>";
		$db->close();
?>
